package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.OrdersEntity;
import com.ie.mapper.admin.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, OrdersEntity> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public ResponseResult<Map<String, Object>> getOrdersByUid(OrdersEntity ordersEntity) {
        // 创建分页对象iPage(PageSize=5)
        Integer currentPage = ordersEntity.getCurrentPage();
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        IPage<OrdersEntity> iPage = new Page<>(currentPage, 5);

        // 创建LambdaQueryWrapper条件构造器
        LambdaQueryWrapper<OrdersEntity> wrapper = new LambdaQueryWrapper<>();

        // 添加id等于条件到Wrapper
        Integer busertableId = ordersEntity.getBusertableId();
        if (busertableId != null && busertableId > 0) {
            wrapper.eq(OrdersEntity::getBusertableId, busertableId);
        }

        // 执行分页查询page(iPage,wrapper)
        IPage<OrdersEntity> page = ordersMapper.selectPage(iPage, wrapper);

        // 创建返回结果Map
        Map<String, Object> resultMap = new HashMap<>();

        // 将查询结果放入Map("ordersByUid")
        resultMap.put("ordersByUid", page.getRecords());
        resultMap.put("totalPage", page.getPages());

        // 返回成功响应结果
        return ResponseResult.getSuccessResult(resultMap);
    }
}